<script setup>
import {
  getBizHousingInfo,
  addBizHousingInfo,
  updateBizHousingInfo,
  findMaxSort
} from "@/api/biz/bizHousingInfo";
import { getAreaList } from "@/api/system/area";
const props = defineProps({
  id: {
    type: [Number, String],
    default: ""
  }
});

const formRef = ref();
const data = ref({
  loading: false,
  form: {
    id: props.id
  },
  rules: {
    housingName: [
      { required: true, message: "请输入小区名称", trigger: "blur" }
    ]
  },
  areasList: []
});

const areasProps = {
  lazy: true,
  lazyLoad(node, resolve) {
    const { level, value } = node;
    setTimeout(() => {
      getAreaList({ level: level, code: value }).then((res) => {
        const nodes = res.data.map((item) => ({
          value: item.code,
          label: item.label,
          leaf: level >= 2
        }));
        resolve(nodes);
      });
    }, 500);
  }
};
onMounted(() => {
  if (data.value.form.id != "") {
    getInfo();
  } else {
    findMaxSort().then((res) => {
      data.value.form.sort = res.data;
    });
  }
});

function getInfo() {
  data.value.loading = true;
  getBizHousingInfo(props.id).then((res) => {
    data.value.loading = false;
    data.value.form = res.data;
    data.value.form.areasCode = JSON.parse(data.value.form.areasCode);
  });
}

function getcheckUserNameUnique(rule, value, callback) {
  if (data.value.form.id !== "" || data.value.form.id !== undefined) {
    callback();
  }
  const msg = "已存在";
  if (value === "") {
    callback();
  } else {
    const checkData = {
      userName: value
    };
    checkUserNameUnique(checkData).then((response) => {
      if (response.data.code === "1") {
        callback();
      } else {
        callback(msg);
      }
    });
  }
}
defineExpose({
  submit(callback) {
    data.value.form.provinceCode = data.value.form.areasCode[0];
    data.value.form.cityCode = data.value.form.areasCode[1];
    data.value.form.areaCode = data.value.form.areasCode[2];

    data.value.form.status = data.value.form.status ? "0" : "1";
    data.value.form.areasCode = JSON.stringify(data.value.form.areasCode);
    if (data.value.form.id == "") {
      formRef.value.validate((valid) => {
        if (valid) {
          console.log(data.value.form);
          addBizHousingInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "保存成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    } else {
      formRef.value.validate((valid) => {
        if (valid) {
          updateBizHousingInfo(data.value.form).then(() => {
            ElMessage.success({
              message: "修改成功",
              center: true
            });
            callback && callback();
          });
        }
      });
    }
  }
});
</script>

<template>
  <div v-loading="data.loading">
    <el-form
      ref="formRef"
      :model="data.form"
      :rules="data.rules"
      label-width="auto"
      label-suffix=" "
      label-position="right"
      style="padding: 0 20px"
    >
      <el-row :gutter="80">
        <el-col :span="24" style="text-align: center">
          <el-form-item label="选择区域" prop="areasCode">
            <el-cascader
              :props="areasProps"
              v-model="data.form.areasCode"
              style="width: 100%"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="小区名称" prop="housingName">
            <el-input
              placeholder="请输入小区名称"
              v-model="data.form.housingName"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="50">
        <el-col :span="20">
          <el-form-item label="排序" prop="sort">
            <el-input-number
              placeholder="请输入排序"
              v-model="data.form.sort"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="50">
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="data.form.status"
              active-text="否"
              inactive-text="是"
              active-value="1"
              inactive-value="0"
              inline-prompt
              style="
                --el-switch-off-color: #409eff;
                --el-switch-on-color: #ff4949;
              "
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="50">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input
              type="textarea"
              placeholder="请输入备注信息"
              v-model="data.form.remark"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
